package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.mgpos.activities.R;

public class DeptSettingListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<List<String>> listItem;
	private int selectedRow = 0;
	private Bitmap activeTrue;
	private Bitmap activeFalse;

	public DeptSettingListAdapter(Context context, List<List<String>> listItem) {
		this.mInflater = LayoutInflater.from(context);
		this.listItem = listItem;
		activeTrue = BitmapFactory.decodeResource(context.getResources(), R.drawable.check_true);
		activeFalse = BitmapFactory.decodeResource(context.getResources(), R.drawable.check_false);
	}

	public void setSource(List<List<String>> listItem) {
		this.listItem = listItem;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listItem.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listItem.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.row_dept_info, null);
			holder.ll_row = (LinearLayout) convertView.findViewById(R.id.ll_row);
			holder.dep_no = (TextView) convertView.findViewById(R.id.tv_dep_no);
			holder.dep_name = (TextView) convertView.findViewById(R.id.tv_dep_name);
			holder.img_tax = (ImageView) convertView.findViewById(R.id.img_tax);
			holder.img_fs = (ImageView) convertView.findViewById(R.id.img_fs);
			holder.img_sb = (ImageView) convertView.findViewById(R.id.img_sb);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.dep_no.setText(listItem.get(position).get(0));
		holder.dep_name.setText(listItem.get(position).get(1));

		if (listItem.get(position).get(2).endsWith("1") ) {
			holder.img_tax.setImageBitmap(activeTrue);
		} else {
			holder.img_tax.setImageBitmap(activeFalse);
		}

		if (listItem.get(position).get(3).endsWith("1")) {
			holder.img_fs.setImageBitmap(activeTrue);
		} else {
			holder.img_fs.setImageBitmap(activeFalse);
		}

		if (listItem.get(position).get(4).endsWith("1")) {
			holder.img_sb.setImageBitmap(activeTrue);
		} else {
			holder.img_sb.setImageBitmap(activeFalse);
		}

		if (position == selectedRow) {
			holder.ll_row.setBackgroundColor(Color.rgb(255, 219, 16));
		} else {
			holder.ll_row.setBackgroundColor(Color.rgb(204, 204, 204));
		}

		return convertView;
	}

	public final class ViewHolder {
		public LinearLayout ll_row;
		public TextView dep_no;
		public TextView dep_name;
		public ImageView img_tax;
		public ImageView img_fs;
		public ImageView img_sb;

	}

	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex() {
		return selectedRow;
	}
}
